﻿

using Hr.Orc.Client.Helper;
using Microsoft.Win32;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Hr.Orc.Client
{
    /// <summary>
    /// SealWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SealWindow : System.Windows.Window
    {
        private string filePath;
        private float Confidence = 0.9f;
        private SealStamp sealStamp;
        public SealWindow(float confidence)
        {
            sealStamp = new SealStamp();
            Confidence = confidence;
            sealStamp.Confidence = confidence;
         
            InitializeComponent();
            score_lbl.Content = $"需达标分值:{confidence * 100}";

        }
        private void read_btn_Click(object sender, RoutedEventArgs e)
        {
            // 创建OpenFileDialog实例
            OpenFileDialog openFileDialog = new OpenFileDialog();
            // 设置过滤器，例如仅选择文本文件
            openFileDialog.Filter = "图像文件(*.bmp,*.jpg,*.png,*.jpeg)|*.bmp;*.jpg;*.png;*.jpeg";
            // 设置标题
            openFileDialog.Title = "选择文件";
            // 显示对话框
            bool? result = openFileDialog.ShowDialog();

            // 检查用户是否点击了"确定"按钮
            if (result == true)
            {
                 filePath = openFileDialog.FileName;
                string fileName = openFileDialog.SafeFileName;
                var bitmap = new BitmapImage(new Uri(filePath, UriKind.Absolute));
                start_img.Height = start_img.Width / bitmap.Width * bitmap.Height;
                start_img.Source = bitmap;
            }
        }

        private void check_btn_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(filePath)) {
                MessageBox.Show("请先选择文件");
                return;
            }

          var detections=  sealStamp.CheckSeal(filePath);
            Mat src = Cv2.ImRead(filePath);
            bool hasSeal = false;
            foreach (var detection in detections)
            {
                if (detection.Confidence > Confidence)
                {
                    Scalar scalar = Scalar.RandomColor();
                    Cv2.Rectangle(src, detection.Rect, scalar, 4, LineTypes.Link8, 0);
                    Cv2.PutText(src, detection.LabelName, new OpenCvSharp.Point(detection.Rect.X + detection.Rect.Width / 2, detection.Rect.Y + detection.Rect.Height / 2), HersheyFonts.HersheyComplex, 2D, scalar, 4, LineTypes.Link4, false);
                    hasSeal = true;
                }
            }
           var bitmap= BitmapConverter.ToBitmap(src);
            end_img.Source = ToBitmapImage(bitmap);
        }
        private BitmapImage ToBitmapImage(Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, ImageFormat.Png);
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = ms;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit(); bitmapImage.Freeze();
            }
            return bitmapImage;

        }

    }
}
